package com.example.medical_delivery_system.api.delivery;

import com.example.medical_delivery_system.api.vo.OrderDetailVO;
import com.example.medical_delivery_system.api.vo.OrderListVO;
import com.example.medical_delivery_system.common.Constants;
import com.example.medical_delivery_system.config.annotation.TokenToUser;
import com.example.medical_delivery_system.entity.User;
import com.example.medical_delivery_system.service.DeliveryOrderService;
import com.example.medical_delivery_system.util.PageQueryUtil;
import com.example.medical_delivery_system.util.PageResult;
import com.example.medical_delivery_system.util.Result;
import com.example.medical_delivery_system.util.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * BelongsProject: medical_delivery_system
 * Author: Kotoba Trily
 * CreateTime: 2022/12/25 16:33
 * Description:
 * Version: 1.0
 */

public class DeliveryOrderAPI {
    @Resource
    private DeliveryOrderService deliveryOrderService;

    @GetMapping("/order/{orderId}")
    @ApiOperation(value = "订单详情接口", notes = "传参为订单的id")
    public Result<OrderDetailVO> orderDetailPage(@ApiParam(value = "订单的id") @PathVariable("orderId") Long orderId, @TokenToUser User loginUser) {
        return ResultGenerator.genSuccessResult(deliveryOrderService.getOrderDetailByOrderId(orderId, loginUser.getUserId()));
    }

    @GetMapping("/order")
    @ApiOperation(value = "订单列表接口", notes = "传参为页码")
    public Result<PageResult<List<OrderListVO>>> orderList(@ApiParam(value = "页码") @RequestParam(required = false) Integer pageNumber,
                                                           @ApiParam(value = "订单状态") @RequestParam(required = false) String status,
                                                           @TokenToUser User loginUser) {
        Map params = new HashMap(8);
        if (pageNumber == null || pageNumber < 1) {
            pageNumber = 1;
        }
        params.put("userId", loginUser.getUserId());
        params.put("orderStatus", status);
        params.put("page", pageNumber);
        params.put("limit", Constants.ORDER_SEARCH_PAGE_LIMIT);
        //封装分页请求参数
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(deliveryOrderService.getMyOrders(pageUtil));
    }
}
